/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.config.javabeans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.config.javabeans.JBTrigger;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.Properties;


/**
 * Test of JBTrigger class.
 */
public class JBTriggerTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBTriggerTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Mail recipient. */
    private static final String MAIL_ADDRESS = "joe.baf@opiam.org";

    /** Instance of JBTrigger. */
    private JBTrigger jbTrigger = new JBTrigger();

    /**
     * Creates a new JBTriggerTest object.
     *
     * @param arg0  Parameter.
     */
    public JBTriggerTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBTriggerTest constructor error\n");
            msg.append(e.toString());
            _test.displayResultFailure(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBTriggerTest("testBegin"));

        suite.addTest(new JBTriggerTest("testGetClassname"));
        suite.addTest(new JBTriggerTest("testGetOpname"));
        suite.addTest(new JBTriggerTest("testGetName"));
        suite.addTest(new JBTriggerTest("testIsPost"));
        suite.addTest(new JBTriggerTest("testGetParam"));

        suite.addTest(new JBTriggerTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBTriggerTest");

        _test.displayHeaderClass("JBTriggerTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBTriggerTest");
    }

    /**
     * Test the methods: setClassname and getClassname .
     *
     */
    public void testGetClassname()
    {
        _test.displayHeaderMethod("testGetClassname");

        try
        {
            String expected = "opiam.admin.faare.service.services.triggers.MailSenderTrigger";
            jbTrigger.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");

            String result = jbTrigger.getClassname();

            if (expected.equals(result))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetClassname");
    }

    /**
     * Test the methods: setOpname and getOpname.
     *
     */
    public void testGetOpname()
    {
        _test.displayHeaderMethod("testGetOpname");

        try
        {
            String expected = "CREATE";
            jbTrigger.setOpname("CREATE");

            String result = jbTrigger.getOpname();

            if (expected.equals(result))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetOpname");
    }

    /**
     * Test the methods: setName and getName.
     *
     */
    public void testGetName()
    {
        _test.displayHeaderMethod("testGetName");

        try
        {
            String expected = "test_trigger1";
            jbTrigger.setName("test_trigger1");

            String result = jbTrigger.getName();

            if (expected == result)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetName");
    }

    /**
     * Test the methods: setPost and isPost.
     *
     */
    public void testIsPost()
    {
        _test.displayHeaderMethod("testIsPost");

        try
        {
            boolean expected = false;
            jbTrigger.setPost(false);

            boolean result = jbTrigger.isPost();

            if (expected == result)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testIsPost");
    }

    /**
     * Test the methods: addParam and getParam.
     *
     */
    public void testGetParam()
    {
        _test.displayHeaderMethod("testGetParam");

        try
        {
            Properties param = new Properties();
            param.setProperty("mail.smtp.sender.template", MAIL_ADDRESS);

            jbTrigger.addParam("mail.smtp.sender.template", MAIL_ADDRESS);

            Properties result = jbTrigger.getParam();

            assertEquals(param.size(), result.size());

            if (param.size() == result.size())
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultSuccess(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetParam");
    }
}
